package rexsee.core.browser.clazz;

import java.util.ArrayList;

import rexsee.core.browser.Browser;

public class EventList {

	public final ArrayList<String> list = new ArrayList<String>();
	private final Browser mBrowser;
	public EventList(Browser browser) {
		mBrowser = browser;
	}

	public void add(String eventName) {
		if (!list.contains(eventName)) list.add(eventName);
	}
	public void run(String eventName) {
		run(eventName, null);
	}
	public void run(String eventName, String[] arguments) {
		if (!list.contains(eventName)) {
			//mBrowser.exception("EventList", "No event found.");
		} else {
			String js = "javascript:";
			if (arguments == null) {
				js += " try { window." + eventName + "(); } catch (e) {} ";
				js += " try { window." + eventName.toLowerCase() + "(); } catch (e) {} ";
				js += " try { window." + eventName.toUpperCase() + "(); } catch (e) {} ";
			} else {
				String argu = "";
				for (int i = 0; i < arguments.length; i++) {
					argu += (i == 0) ? "'" + arguments[i] + "'" : ",'" + arguments[i] + "'";
				}
				js += " try { window." + eventName + "(" + argu + "); } catch (e) {} ";
				js += " try { window." + eventName.toLowerCase() + "(" + argu + "); } catch (e) {} ";
				js += " try { window." + eventName.toUpperCase() + "(" + argu + "); } catch (e) {} ";
			}
			try {
				mBrowser.function.load(js);
			} catch (Exception e) {
			}
		}
	}

}
